package com.gzx.plugin.tjzy.core.utils.crypter;

import com.google.common.hash.Hashing;

import java.nio.charset.StandardCharsets;

/**
 * 使用AES算法进行加解密的加解密器，
 * 租户隔离
 */
public class WithTenantAesCrypter implements Crypter {
    private final byte[] aesKey;

    public WithTenantAesCrypter(String tenantCode) {
        this.aesKey = Hashing.md5().hashString(tenantCode, StandardCharsets.UTF_8).asBytes();
    }

    @Override
    public Ciphertext crypt(Ciphertext ciphertext) {
        CiphertextWrapper wrapper = CiphertextWrapper.wrap(ciphertext);
        wrapper.setEncryptor(raw -> AesUtil.encryptText(raw, aesKey));
        wrapper.setDecryptor(enc -> AesUtil.decryptText(enc, aesKey));
        wrapper.setMasker(raw -> TextUtil.mask(raw, 3, 4));
        return wrapper;
    }
}
